package algo;

import model.tsp.Problem;
import model.tsp.Solution;

import java.util.List;


public class LocalSearch3 extends LocalSearch {

    public LocalSearch3(Problem problem) {
        super(problem);
    }

    @Override
    protected Solution localSearch(Solution solution) {
        int k = 1;
        int kMax = 6;
        int localSearchCnt = 3000;
        while (k <= kMax) {
            // 局部搜索过程，搜索邻域k中的较优解
            // 每个操作算子随机变换一次
            for (int i = 0; i < localSearchCnt; i++) {
                List<Integer> newX;
                if (k == 1) {
                    newX = neighborhoodOperator1(solution.getPermutation());
                } else if (k == 2) {
                    newX = neighborhoodOperator2(solution.getPermutation());
                } else if (k == 3) {
                    newX = neighborhoodOperator3(solution.getPermutation());
                } else if (k == 4) {
                    newX = neighborhoodOperator4(solution.getPermutation());
                } else if (k == 5) {
                    newX = neighborhoodOperator5(solution.getPermutation());
                } else if (k == 6) {
                    newX = neighborhoodOperator6(solution.getPermutation());
                } else {
                    throw new RuntimeException("超出neighborhood邻域集合长度: " + k);
                }

                Solution newSolution = new Solution(newX, calculateFitness(newX));
                if (newSolution.getCost() < solution.getCost()) {
                    solution = newSolution.copy();
                }
            }

            k++;
        }
        return solution;

    }


}
